<?php
namespace Modules\Activity\Http\Controllers\Client\Lottery;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use \Modules\Activity\Entities\Lottery\Activity;
use \Modules\Activity\Entities\Lottery\InviteeRecord;
use \Modules\User\Entities\User;

use Validator;
use ThrowException;
use Auth;
use DB;

// 参与活动的用户（is_lucker_user）
class InviteController extends Controller
{
    // 某个人某个活动的助力记录
    public function myInviteeIndex(Request $req, $uuid)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $options = $req->all();
        $user = Auth::requireLoginUser();

        $activity = Activity::getEntity([
            'uuid' => $uuid
        ]);

        $list = InviteeRecord::getList([
            'where' => [
                'activity_id' => $activity->id,
                'user_id' => $user->id
            ],
            'with' => ['invitee'],
            'only' => [
                'created_at',
                'ticket_total',
                'invitee' => [
                    'name',
                    'headimg'
                ]
            ]
        ]);

        return $list->generateListResponse();
    }

    // 某个用户的邀请信息
    public function inviteInfo(Request $req, $uuid)
    {
        $rule = [
            'inviter' => ['required']
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $activity = Activity::getEntity([
            'uuid' => $uuid
        ]);

        $inviter = User::getEntity([
            'uuid' => $req->inviter
        ]);

        $record = $activity->records()->where('user_id', $inviter->id)->first();

        // 是否已助力过
        $isAccept = $activity->isAcceptedInvite($inviter, Auth::user()) ? 1 : 0;

        $userTicketTotal = $activity->tickets()->where('user_id', $inviter->id)->count();
        $allTicketTotal = $activity->tickets()->count();

        $res = [
            'activity' => [
                'id' => $activity->id,
                'uuid' => $activity->uuid,
            ],
            'record_id' => @$record->id,
            'user' => [
                'uuid' => $inviter->uuid,
                'name' => $inviter->name,
                'headimg' => $inviter->headimg
            ],
            'is_accepted' => $isAccept,
            'user_ticket_total' => $userTicketTotal,
            'all_ticket_total' => $allTicketTotal,
            'old_user_reward_ticket' => $activity->old_user_reward_ticket,
            'new_user_reward_ticket' => $activity->new_user_reward_ticket,
        ];

        return $res;
    }


    // 帮好友助力
    public function acceptInvite(Request $req, $uuid)
    {

        $rule = [
            'inviter' => ['required']
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $user = Auth::requireLoginUser();

        $activity = Activity::getEntity([
            'uuid' => $uuid
        ]);

        if ($activity->union_status === 'expired') {
            ThrowException::Conflict('活动已结束了哦~');
        }

        $inviter = User::getEntity([
            'uuid' => $req->inviter
        ]);

        $activity->invitees()->where('invitee_id', $user->id)->where('session', $activity->current_session)->first() && ThrowException::Conflict('你已在此期活动助力过了哦~');

        // 增加助力记录
        DB::beginTransaction();

        $activity->addInvite($inviter, $user);

        DB::commit();

        return [];
    }
}
